package de.sendorian.app.forumArchive.renamer;

import de.sendorian.app.forumArchive.domain.File;
import de.sendorian.util.matcher.RegexMatcher;


public class ForumHashRemoverFileRenamer implements FileRenamer {

    private int hashLength;

    private RegexMatcher regexMatcher;


    public ForumHashRemoverFileRenamer() {
        this(3);
    }


    public ForumHashRemoverFileRenamer(int hashLength) {
        this.hashLength = hashLength;
        regexMatcher = new RegexMatcher("^\\w{" + hashLength + "}_.*");
    }


    public File rename(File file) {
        String fileName = file.getName();
        if (!file.isExternal()) {
            fileName = cutOffLeadingForumNumbers(fileName);
        }
        file.setName(fileName);
        return file;
    }


    private String cutOffLeadingForumNumbers(String fileName) {
        if (regexMatcher.matches(fileName))
            return fileName.substring(hashLength + 1, fileName.length());
        return fileName;
    }

}
